//
//  DrugResultViewController.m
//  TestARC
//
//  Created by wuxiaoquan on 12-9-29.
//
//

#import "DrugResultViewController.h"
#import "DrugDetailViewController.h"
#import "DBHelper.h"

@interface DrugResultViewController ()

@end

@implementation DrugResultViewController

@synthesize resultTableView;
@synthesize drugQueryId,resultArray,useFor;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    if ([self.useFor isEqualToString:@"searchWithName"]) {
        
    }
    else
    {
        self.resultArray = [NSMutableArray arrayWithCapacity:5];
        [self setDrugView];
    }
    [self addRoundBound:resultTableView];
}

- (void)setDrugView
{
    DBHelper *myDBHelper = [[DBHelper alloc]init];
    if ([myDBHelper open]) {
        //NSLog(@"open db ok!");
    } else {
        NSLog(@"can not open db!");
    }
    
    
    [self.resultArray removeAllObjects];
    FMResultSet *rs =[myDBHelper queryDrugsLike:self.drugQueryId];
    
    while ([rs next]) {
        [self.resultArray addObject:[rs resultDictionary]];
    }

    [self.resultTableView reloadData];
    
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
    self.resultTableView = nil;
    self.drugQueryId = nil;
    self.useFor = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

//-------------------------表格数据----------------------------------
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.resultArray count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";
    UITableViewCell *cell = nil;
    NSInteger row;
    
    cell = [tableView dequeueReusableCellWithIdentifier:SimpleTableIdentifier];
    if (cell==nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault
                                     reuseIdentifier:SimpleTableIdentifier];
    }
    row = [indexPath row];
    cell.textLabel.text = [[self.resultArray objectAtIndex:row]objectForKey:@"name"];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.textLabel.font = [UIFont systemFontOfSize:17.0f];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //填装详细视图的内容。
    
    DrugDetailViewController * detailController = [[DrugDetailViewController alloc]initWithNibName:@"DrugDetailViewController" bundle:nil];
    
    int row = [indexPath row];
    //NSLog(@"选择了 %d 行",row);
    NSDictionary *drug;
    drug = [self.resultArray objectAtIndex:row];
    detailController.name =       [drug objectForKey:@"name"];
    detailController.brandName =  [drug objectForKey:@"BrandName"];
    detailController.commonName = [drug objectForKey:@"CommonName"];
    detailController.queryId =    [drug objectForKey:@"queryid"];
    
    [self.navigationController pushViewController:detailController animated:YES];
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}


@end
